package com.woniu.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 结果集类，返回给前端的数据模型
 * @CreateTime: 2023-02-22 23:50
 * @Description: 枚举 封装就是状态码
 * @author Wen.j 434679924@qq.com
 */
@Data
@ApiModel(value = "结果集对象")
public class HttpResponseResults<T> {

    @ApiModelProperty(value = "状态码")
    private int code; // 200
    @ApiModelProperty(value = "返回的消息说明")
    private String msg; // 请求成功
    @ApiModelProperty("返回的数据")
    private T data; // 数据的

    // 添加了两个缓存，一个是成功的，一个是失败的
    //  泛型的上边界

    // 参考了 Boolean这个类的设计思想 ->
    public static final HttpResponseResults<? extends Object> DEFAULT_SUCCESS =  HttpResponseResults.success(HttpResultCode.SUCCESS);

    public static final HttpResponseResults<? extends Object> DEFAULT_FAIL =  HttpResponseResults.fail(HttpResultCode.FAIL);


    private HttpResponseResults() {}

    private HttpResponseResults(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static HttpResponseResults success(HttpResultCode resultCode){
        HttpResponseResults resultCommon = new HttpResponseResults(resultCode.getCode(), resultCode.getMsg());
        return resultCommon;
    }


    public static <T> HttpResponseResults success(HttpResultCode resultCode, T data){
        HttpResponseResults success = success(resultCode);
        success.setData(data);
        return success;
    }

    public static HttpResponseResults fail(HttpResultCode resultCode){
        return success(resultCode);
    }



    public enum HttpResultCode {

        SUCCESS(200,"请求成功"),
        NO_DELETE(800,"数据在使用中，勿删除"),
        NO_DELETE_PARENT(801,"父级分类下有子分类，勿删除!"),
        NO_ONLINE(803,"该商品没有审核通过，不允许上下架!"),
        CART_200(804,"该商品在购物车中数量最多为200!"),
        FAIL(500,"请求失败"),
        TIMEOUT(505,"支付超时"),
        REGISTER_SUCCESS(200,"注册成功"),
        USERNAME_NO_REPEAT(200,"用户名可用"),
        ADD_FAV_SUCCESS(200,"收藏成功!"),
        REMOVE_SUCCESS(200,"移除成功!"),
        CODE_FAIL(9001,"验证码错误"),
        LOGIN_FAIL(9002,"用户名或密码错误"),
        NO_LOGIN(9003,"请先登录"),
        NO_CHECK(9090,"验证失败"),
        REGISTER_FAIL(9005," 注册失败"),
        USERNAME_REPEAT(9006," 用户名重复"),
        LOGIN_FORBID(9007,"用户被禁用"),
        NO_RIGTHS(403,"暂无权限操作"),
        @SuppressWarnings("all")
        REPASSWORD_ERROR(9004,"两次密码不一致"),
        PASSWORD_ERROR(9008,"原密码错误"),
        PASSWORD_EMPTY(9009,"密码不能为空"),
        STOCK_NO(9010,"库存不足");

        private int code;
        private String msg;

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }

        HttpResultCode(int code, String msg){
            this.code=code;
            this.msg=msg;
        }
    }

}
